package com.lineage.data.cmd;

import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.lock.CharShiftingReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_HelpMessage;
import com.lineage.server.serverpackets.S_ItemCount;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.DigitalUtil;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CreateNewItem {
  private static final Log _log = LogFactory.getLog(CreateNewItem.class);
  
  public static boolean delItems(L1PcInstance pc, int[] srcItemIds, int[] counts, long amount) {
    try {
      if (pc == null)
        return false; 
      if (amount <= 0L)
        return false; 
      if (srcItemIds.length <= 0)
        return false; 
      if (counts.length <= 0)
        return false; 
      if (srcItemIds.length != counts.length) {
        _log.error("道具交换物品与数量阵列设置异常!");
        return false;
      } 
      int i = 0;
      while (i < srcItemIds.length) {
        long itemCount = counts[i] * amount;
        L1ItemInstance item = pc.getInventory().checkItemX(srcItemIds[i], itemCount);
        if (item == null)
          return false; 
        i++;
      } 
      i = 0;
      while (i < srcItemIds.length) {
        long itemCount2 = counts[i] * amount;
        L1ItemInstance item = pc.getInventory().checkItemX(srcItemIds[i], itemCount2);
        if (item == null)
          return false; 
        pc.getInventory().removeItem(item, itemCount2);
        i++;
      } 
      return true;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
  }
  
  public static boolean getItem(L1PcInstance pc, L1NpcInstance npc, String cmd, int[] items, int[] counts, int[] gitems, int[] gcounts, long amount) {
    long xcount = checkNewItem(pc, items, counts);
    if (xcount <= 0L)
      return true; 
    if (amount == 0L) {
      pc.sendPackets((ServerBasePacket)new S_ItemCount(npc.getId(), (int)xcount, cmd));
      return false;
    } 
    if (xcount >= amount)
      createNewItem(pc, items, counts, gitems, amount, gcounts); 
    return true;
  }
  
  public static long checkNewItem(L1PcInstance pc, int srcItemId, int count) {
    try {
      if (pc == null)
        return -1L; 
      L1ItemInstance item = pc.getInventory().findItemIdNoEq(srcItemId);
      long itemCount = -1L;
      if (item != null)
        itemCount = item.getCount() / count; 
      if (itemCount < 1L) {
        L1Item tgItem = ItemTable.get().getTemplate(srcItemId);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(tgItem.getNameId())) + "(" + (
              count - ((item == null) ? 0L : item.getCount())) + ")"));
        return -1L;
      } 
      return itemCount;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return -1L;
    } 
  }
  
  public static long checkNewItem(L1PcInstance pc, int[] srcItemIds, int[] counts) {
    try {
      if (pc == null)
        return -1L; 
      if (srcItemIds.length <= 0)
        return -1L; 
      if (counts.length <= 0)
        return -1L; 
      if (srcItemIds.length != counts.length) {
        _log.error("道具交换物品与数量阵列设置异常!");
        return -1L;
      } 
      long[] checkCount = new long[srcItemIds.length];
      boolean error = false;
      int i = 0;
      while (i < srcItemIds.length) {
        int itemid = srcItemIds[i];
        int count = counts[i];
        L1ItemInstance item = pc.getInventory().findItemIdNoEq(itemid);
        if (item != null) {
          long itemCount = item.getCount() / count;
          checkCount[i] = itemCount;
          if (itemCount < 1L) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, 
                  String.valueOf(String.valueOf(item.getName())) + "(" + (count - item.getCount()) + ")"));
            error = true;
          } 
        } else {
          L1Item tgItem = ItemTable.get().getTemplate(itemid);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(tgItem.getNameId())) + "(" + count + ")"));
          error = true;
        } 
        i++;
      } 
      if (!error) {
        long count2 = DigitalUtil.returnMin(checkCount);
        return count2;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return -1L;
  }
  
  public static void createNewItem(L1PcInstance pc, int[] srcItemIds, int[] counts, int getItemId, long amount, int getCount) {
    try {
      if (pc == null)
        return; 
      if (amount <= 0L)
        return; 
      if (srcItemIds.length <= 0)
        return; 
      if (counts.length <= 0)
        return; 
      if (srcItemIds.length != counts.length) {
        _log.error("道具交换物品与数量阵列设置异常!");
        return;
      } 
      if (getItemId == 0)
        return; 
      if (getCount == 0)
        return; 
      boolean error = false;
      int i = 0;
      while (i < srcItemIds.length) {
        long itemCount = counts[i] * amount;
        L1ItemInstance item = pc.getInventory().checkItemX(srcItemIds[i], itemCount);
        if (item == null)
          error = true; 
        i++;
      } 
      if (!error && !getItemIsOk(pc, getItemId, amount, getCount))
        error = true; 
      if (!error) {
        i = 0;
        while (i < srcItemIds.length) {
          long itemCount2 = counts[i] * amount;
          L1ItemInstance item = pc.getInventory().checkItemX(srcItemIds[i], itemCount2);
          if (item != null) {
            pc.getInventory().removeItem(item, itemCount2);
          } else {
            error = true;
          } 
          i++;
        } 
      } 
      if (!error)
        giveItem(pc, getItemId, amount, getCount); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void createNewItem(L1PcInstance pc, int[] srcItemIds, int[] counts, int[] getItemIds, long amount, int[] getCounts) {
    try {
      if (pc == null)
        return; 
      if (amount <= 0L)
        return; 
      if (srcItemIds.length <= 0)
        return; 
      if (counts.length <= 0)
        return; 
      if (srcItemIds.length != counts.length) {
        _log.error("道具交换物品与数量阵列设置异常!");
        return;
      } 
      if (getItemIds.length <= 0)
        return; 
      if (getCounts.length <= 0)
        return; 
      if (getItemIds.length != getCounts.length) {
        _log.error("道具交换物品与数量阵列设置异常!");
        return;
      } 
      boolean error = false;
      int i = 0;
      while (i < srcItemIds.length) {
        long itemCount = counts[i] * amount;
        L1ItemInstance item = pc.getInventory().checkItemX(srcItemIds[i], itemCount);
        if (item == null)
          error = true; 
        i++;
      } 
      if (!error) {
        i = 0;
        while (i < getItemIds.length) {
          if (!getItemIsOk(pc, getItemIds[i], amount, getCounts[i]))
            error = true; 
          i++;
        } 
      } 
      if (!error) {
        i = 0;
        while (i < srcItemIds.length) {
          long itemCount2 = counts[i] * amount;
          L1ItemInstance item = pc.getInventory().checkItemX(srcItemIds[i], itemCount2);
          if (item != null) {
            pc.getInventory().removeItem(item, itemCount2);
          } else {
            error = true;
          } 
          i++;
        } 
      } 
      if (!error) {
        i = 0;
        while (i < getItemIds.length) {
          giveItem(pc, getItemIds[i], amount, getCounts[i]);
          i++;
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private static boolean getItemIsOk(L1PcInstance pc, int getItemId, long amount, int getCount) {
    try {
      if (pc == null)
        return false; 
      L1Item tgItem = ItemTable.get().getTemplate(getItemId);
      if (pc.getInventory().checkAddItem(tgItem, amount * getCount) != 0)
        return false; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return true;
  }
  
  private static void giveItem(L1PcInstance pc, int getItemId, long amount, int getCount) {
    try {
      if (pc == null)
        return; 
      L1Item tgItem = ItemTable.get().getTemplate(getItemId);
      if (tgItem.isStackable()) {
        L1ItemInstance tgItemX = ItemTable.get().createItem(getItemId);
        tgItemX.setCount(amount * getCount);
        createNewItem(pc, tgItemX);
      } else {
        int get = 0;
        while (get < amount * getCount) {
          L1ItemInstance tgItemX2 = ItemTable.get().createItem(getItemId);
          tgItemX2.setCount(1L);
          createNewItem(pc, tgItemX2);
          get++;
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void createNewItem(L1PcInstance pc, int srcItemId, int count, int getItemId, int getCount) {
    createNewItem(pc, srcItemId, count, getItemId, 1L, getCount);
  }
  
  public static void createNewItem(L1PcInstance pc, int srcItemId, int count, int getItemId, long amount, int getCount) {
    long itemCount1 = count * amount;
    L1ItemInstance item1 = pc.getInventory().checkItemX(srcItemId, itemCount1);
    if (item1 != null) {
      L1ItemInstance tgItem = ItemTable.get().createItem(getItemId);
      if (pc.getInventory().checkAddItem(tgItem, amount * getCount) == 0) {
        pc.getInventory().removeItem(item1, itemCount1);
        if (tgItem.isStackable()) {
          tgItem.setCount(amount * getCount);
          createNewItem(pc, tgItem);
        } else {
          int get = 0;
          while (get < amount * getCount) {
            L1ItemInstance tgItemX = ItemTable.get().createItem(getItemId);
            tgItemX.setCount(1L);
            createNewItem(pc, tgItemX);
            get++;
          } 
        } 
      } else {
        World.get().removeObject((L1Object)tgItem);
      } 
    } 
  }
  
  public static void createNewItem(L1PcInstance pc, int srcItemId, int count, int getItemId, long amount) {
    long itemCount1 = count * amount;
    L1ItemInstance item1 = pc.getInventory().checkItemX(srcItemId, itemCount1);
    if (item1 != null) {
      L1ItemInstance tgItem = ItemTable.get().createItem(getItemId);
      if (pc.getInventory().checkAddItem(tgItem, amount) == 0) {
        pc.getInventory().removeItem(item1, itemCount1);
        if (tgItem.isStackable()) {
          tgItem.setCount(amount);
          createNewItem(pc, tgItem);
        } else {
          int get = 0;
          while (get < amount) {
            L1ItemInstance tgItemX = ItemTable.get().createItem(getItemId);
            tgItemX.setCount(1L);
            createNewItem(pc, tgItemX);
            get++;
          } 
        } 
      } 
    } 
  }
  
  public static void createNewItem(L1PcInstance pc, L1ItemInstance item) {
    try {
      if (pc == null)
        return; 
      if (item == null)
        return; 
      pc.getInventory().storeItem(item);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void createNewItem(L1PcInstance pc, L1ItemInstance item, long count) {
    try {
      if (pc == null)
        return; 
      if (item == null)
        return; 
      item.setCount(count);
      if (pc.getInventory().checkAddItem(item, count) == 0) {
        pc.getInventory().storeItem(item);
      } else {
        item.set_showId(pc.get_showId());
        World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
      } 
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static boolean createNewItem(L1PcInstance pc, int item_id, long count) {
    try {
      if (pc == null)
        return false; 
      L1ItemInstance item = ItemTable.get().createItem(item_id);
      if (item != null) {
        item.setCount(count);
        if (pc.getInventory().checkAddItem(item, count) == 0) {
          pc.getInventory().storeItem(item);
        } else {
          item.set_showId(pc.get_showId());
          World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
        return true;
      } 
      _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
      return false;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
  }
  
  public static boolean createNewItemAi(L1PcInstance pc, int item_id, long count) {
    try {
      if (pc == null)
        return false; 
      L1ItemInstance item = ItemTable.get().createItem(item_id);
      if (item != null) {
        item.setCount(count);
        if (pc.getInventory().checkAddItem(item, count) == 0) {
          pc.getInventory().storeItem(item);
        } else {
          item.set_showId(pc.get_showId());
          World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
        return true;
      } 
      _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
      return false;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
  }
  
  public static void getQuestItem(L1Character atk, L1NpcInstance npc, int item_id, long count) {
    try {
      if (atk == null)
        return; 
      L1ItemInstance item = ItemTable.get().createItem(item_id);
      if (item != null) {
        item.setCount(count);
        if (atk.getInventory().checkAddItem(item, count) == 0) {
          atk.getInventory().storeItem(item);
        } else {
          item.set_showId(atk.get_showId());
          World.get().getInventory(atk.getX(), atk.getY(), atk.getMapId()).storeItem(item);
        } 
        if (atk instanceof L1PcInstance) {
          L1PcInstance pc = (L1PcInstance)atk;
          if (npc != null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(143, npc.getNameId(), item.getLogName()));
            pc.sendPackets((ServerBasePacket)new S_HelpMessage("\\fW" + npc.getNameId() + "给你" + item.getLogName()));
          } else {
            pc.sendPackets((ServerBasePacket)new S_HelpMessage("\\fW给你", item.getLogName()));
          } 
        } 
      } else {
        _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void updateA(L1PcInstance pc, L1ItemInstance srcItem, L1ItemInstance newItem, int enchant, int down, int mode) {
    try {
      if (pc == null)
        return; 
      if (srcItem == null)
        return; 
      if (newItem == null)
        return; 
      newItem.setCount(1L);
      if (srcItem.getEnchantLevel() > enchant) {
        newItem.setEnchantLevel(srcItem.getEnchantLevel() - down);
      } else {
        newItem.setEnchantLevel(srcItem.getEnchantLevel());
      } 
      newItem.setAttrEnchantKind(srcItem.getAttrEnchantKind());
      newItem.setAttrEnchantLevel(srcItem.getAttrEnchantLevel());
      newItem.setIdentified(true);
      int srcObjid = srcItem.getId();
      L1Item srcItemX = srcItem.getItem();
      if (pc.getInventory().removeItem(srcItem) == 1L) {
        pc.getInventory().storeItem(newItem);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, newItem.getLogName()));
        CharShiftingReading.get().newShifting(pc, 0, null, srcObjid, srcItemX, newItem, mode);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void updateB(L1PcInstance pc, L1ItemInstance srcItem, int newid) {
    try {
      if (pc == null)
        return; 
      if (srcItem == null)
        return; 
      L1ItemInstance newItem = ItemTable.get().createItem(newid);
      if (newItem != null) {
        if (pc.getInventory().removeItem(srcItem) == 1L)
          pc.getInventory().storeItem(newItem); 
      } else {
        _log.error("给予物件失败 原因: 指定编号物品不存在(" + newid + ")");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void createNewItem_LV(L1PcInstance pc, int item_id, int count, int EnchantLevel) {
    L1ItemInstance item = ItemTable.get().createItem(item_id);
    item.setCount(count);
    item.setEnchantLevel(EnchantLevel);
    item.setIdentified(true);
    if (pc.getInventory().checkAddItem(item, count) == 0) {
      pc.getInventory().storeItem(item);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
    } else {
      World.get().removeObject((L1Object)item);
    } 
  }
  
  public static boolean createNewItem(L1PcInstance pc, int item_id, int count, int en) {
    try {
      if (pc == null)
        return false; 
      L1ItemInstance item = ItemTable.get().createItem(item_id);
      if (item != null) {
        if (item.isStackable()) {
          item.setCount(count);
        } else {
          item.setCount(1L);
        } 
        item.setEnchantLevel(en);
        if (pc.getInventory().checkAddItem(item, count) == 0) {
          pc.getInventory().storeItem(item);
        } else {
          World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
        return true;
      } 
      _log.error("给予物件失败 原因: 指定编号物品不存在(" + item_id + ")");
      return false;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
  }
  
  public static void createNewItem_NPC(L1PcInstance pc, String npcName, int item_id, int count) {
    L1ItemInstance item = ItemTable.get().createItem(item_id);
    item.setCount(count);
    if (pc.getInventory().checkAddItem(item, count) == 0) {
      pc.getInventory().storeItem(item);
    } else {
      World.get().getInventory(pc.getX(), pc.getY(), pc.getMapId()).storeItem(item);
    } 
    pc.sendPackets((ServerBasePacket)new S_ServerMessage(143, npcName, item.getLogName()));
  }
}
